package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class LoginController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private KeeperService keeperService;
    @Autowired
    private DormitoryAdminService dormitoryAdminService;
    @Autowired
    private LeaderService leaderService;
    @Autowired
    private SuperAdminService superAdminService;

    @PostMapping("/login")
    public JsonResponse login(@RequestBody User user,
                              HttpServletRequest request,
                              HttpServletResponse response){
        String username = user.getUsername();
        String password = user.getPassword();
        if(username.isEmpty()){
            return JsonResponse.failure("Error: Empty user name!");
        }


        char s = username.charAt(0);
        // 学生
        if(s == 's'){
            LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Student::getIsDeleted, false).eq(Student::getAccount, username).eq(Student::getPassword, password);
            Student student = studentService.getBaseMapper().selectOne(wrapper);
            if(student!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(student, String.format("Student %s login Success.", student.getAccount()));
            }
        }
        // 老师
        else if(s == 't'){
            LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teacher::getIsDeleted, false).eq(Teacher::getAccount, username).eq(Teacher::getPassword, password);
            Teacher teacher = teacherService.getBaseMapper().selectOne(wrapper);
            if(teacher!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(teacher, String.format("Teacher %s login Success.", teacher.getAccount()));
            }
        }
        // 宿管
        else if(s == 'd'){
            LambdaQueryWrapper<DormitoryAdmin> wrapper  = new LambdaQueryWrapper<>();
            wrapper.eq(DormitoryAdmin::getIsDeleted, false).eq(DormitoryAdmin::getAccount, username).eq(DormitoryAdmin::getPassword, password);
            DormitoryAdmin  dormitoryAdmin = dormitoryAdminService.getBaseMapper().selectOne(wrapper);
            if(dormitoryAdmin!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(dormitoryAdmin, String.format("dormitoryAdmin %s login Success.", dormitoryAdmin.getAccount()));
            }
        }
        // 维修人员
        else if(s == 'k'){
            LambdaQueryWrapper<Keeper> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Keeper::getIsDeleted, false).eq(Keeper::getAccount, username).eq(Keeper::getPassword, password);
            Keeper  keeper = keeperService.getBaseMapper().selectOne(wrapper);
            if(keeper!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(keeper, String.format("Keeper %s login Success.", keeper.getAccount()));
            }
        }
        // 领导
        else if(s == 'l'){
            LambdaQueryWrapper<Leader> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Leader::getIsDeleted, false).eq(Leader::getAccount, username).eq(Leader::getPassword, password);
            Leader leader = leaderService.getBaseMapper().selectOne(wrapper);
            if(leader!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(leader, String.format("Leader %s login Success.", leader.getAccount()));
            }
        }
        else if(s == 'a'){
            LambdaQueryWrapper<SuperAdmin> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SuperAdmin::getIsDeleted, false).eq(SuperAdmin::getAccount, username).eq(SuperAdmin::getPassword, password);
            SuperAdmin superAdmin = superAdminService.getBaseMapper().selectOne(wrapper);
            if(superAdmin!=null){
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(superAdmin, String.format("Leader %s login Success.", superAdmin.getAccount()));
            }
        }
        return JsonResponse.failure("Error: Unknown account or password");
    }

    @PostMapping("/modifyPassword")
    public JsonResponse modifyPassword(@RequestBody User user,
                                       HttpServletRequest request,
                                       HttpServletResponse response){
        String username = user.getUsername();
        String password = user.getPassword();
        String newPassword = user.getNewPassword();
        if(username == null||password == null||newPassword == null){
            return JsonResponse.failure("Error!");
        }
        char s = username.charAt(0);
        System.out.println(s);
        // 学生
        if(s == 's'){
            LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Student::getIsDeleted, false).eq(Student::getAccount, username).eq(Student::getPassword, password);
            Student student = studentService.getBaseMapper().selectOne(wrapper);
            if(student!=null){
                student.setPassword(newPassword);
                studentService.getBaseMapper().update(student, wrapper);
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(true, String.format("Student %s modify Success.", student.getAccount()));
            }
        }
        // 老师
        else if(s == 't'){
            LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teacher::getIsDeleted, false).eq(Teacher::getAccount, username).eq(Teacher::getPassword, password);
            Teacher teacher = teacherService.getBaseMapper().selectOne(wrapper);
            if(teacher!=null){
                teacher.setPassword(newPassword);
                teacherService.getBaseMapper().update(teacher, wrapper);
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(true, String.format("Teacher %s modify Success.", teacher.getAccount()));
            }
        }
        // 宿管
        else if(s == 'd'){
            LambdaQueryWrapper<DormitoryAdmin> wrapper  = new LambdaQueryWrapper<>();
            wrapper.eq(DormitoryAdmin::getIsDeleted, false).eq(DormitoryAdmin::getAccount, username).eq(DormitoryAdmin::getPassword, password);
            DormitoryAdmin  dormitoryAdmin = dormitoryAdminService.getBaseMapper().selectOne(wrapper);
            if(dormitoryAdmin!=null){
                dormitoryAdmin.setPassword(newPassword);
                dormitoryAdminService.getBaseMapper().update(dormitoryAdmin,wrapper);
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(true, String.format("dormitoryAdmin %s modify Success.", dormitoryAdmin.getAccount()));
            }
        }
        // 维修人员
        else if(s == 'k'){
            LambdaQueryWrapper<Keeper> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Keeper::getIsDeleted, false).eq(Keeper::getAccount, username).eq(Keeper::getPassword, password);
            Keeper  keeper = keeperService.getBaseMapper().selectOne(wrapper);
            if(keeper!=null){
                keeper.setPassword(newPassword);
                keeperService.getBaseMapper().update(keeper, wrapper);
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(true, String.format("Keeper %s modify Success.", keeper.getAccount()));
            }
        }
        // 领导
        else if(s == 'l'){
            LambdaQueryWrapper<Leader> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Leader::getIsDeleted, false).eq(Leader::getAccount, username).eq(Leader::getPassword, password);
            Leader leader = leaderService.getBaseMapper().selectOne(wrapper);
            if(leader!=null){
                leader.setPassword(newPassword);
                leaderService.getBaseMapper().update(leader, wrapper);
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60*60*2);
                session.setAttribute("user", user);
                return JsonResponse.success(true, String.format("Leader %s login Success.", leader.getAccount()));
            }
        }
        return JsonResponse.failure("Error password");
    }



}
